package ru.aslanov.schedule.client.ds;

import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.types.FieldType;
import ru.aslanov.schedule.client.ScheduleUser;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * Created: Feb 2, 2010 3:49:21 PM
 *
 * @author Sergey Aslanov
 */
public class SchedulesDS extends BaseDS {
    private static SchedulesDS instance = null;
    private static Map<String, SchedulesDS> schedulesDS = new HashMap<String, SchedulesDS>();

    public static SchedulesDS getInstance(ScheduleUser scheduleUser) {
        SchedulesDS dataSource = schedulesDS.get(scheduleUser.getScheduleKey());
        if (dataSource == null) {
            dataSource = new SchedulesDS("Schedule" + scheduleUser.getScheduleKey(), "Schedule", scheduleUser);
            schedulesDS.put(scheduleUser.getScheduleKey(), dataSource);
        }
        return dataSource;
    }

    public static SchedulesDS getInstance() {
        if (instance == null) {
            instance = new SchedulesDS("Schedule", "Schedule", null);
        }
        return instance;
    }

    public SchedulesDS(String id, String dataId, InputLangProvider inputLangProvider) {
        super(id, dataId, inputLangProvider);

        DataSourceField name = new DataSourceField("name", FieldType.TEXT, cnt.nameField());
        name.setRequired(false);

        DataSourceField scheduleAdmin = new DataSourceField("isScheduleAdmin", FieldType.BOOLEAN);
        scheduleAdmin.setHidden(true);

        DataSourceField teacherKey = new DataSourceField("teacherKey", FieldType.TEXT);
        teacherKey.setHidden(true);

        DataSourceField defaultInputLanguage = new DataSourceField("defaultInputLanguage", FieldType.TEXT);
        defaultInputLanguage.setHidden(true);

        DataSourceField hasSessionToken = new DataSourceField("hasSessionToken", FieldType.BOOLEAN);
        hasSessionToken.setHidden(true);

        DataSourceField lastGCalPublishDate = new DataSourceField("lastGCalPublishDate", FieldType.DATETIME);
        lastGCalPublishDate.setHidden(true);

        DataSourceField city = new DataSourceField("city", FieldType.TEXT, cnt.cityField());

        DataSourceField publishUrl = new DataSourceField("publishUrl", FieldType.TEXT, cnt.publishUrlField(), 2000);

        setFields(encodedKey, name, scheduleAdmin, teacherKey, hasSessionToken, lastGCalPublishDate, city, publishUrl, defaultInputLanguage);
    }
}